#include "json.h"

using namespace std;

string get_current_time(void)
{
    time_t now = time(NULL);
    // 转换为本地时间（结构体 tm 包含年月日时分秒）
    struct tm* local_time = localtime(&now); 

    // 格式化时间字符串（注意：strftime 会自动添加 '\0' 终止符）
    char time_str[20];
    // 格式说明：%Y=年，%m=月，%d=日，%H=时（24小时制），%M=分，%S=秒
    strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", local_time);

    // std::cout << "当前本地时间：" << time_str << std::endl; // 例如：2025-09-10 15:30:45
    return time_str;
}

void rapidjson_parse_json(string &payload)
{
    rapidjson::Document doc;
    if(!doc.Parse(payload.data()).HasParseError())
    {
        if(doc.HasMember("theme") && doc["theme"].IsString()) { printf("theme: %s\n", doc["theme"].GetString()); }
        if(doc.HasMember("time")  && doc["time"].IsString())  { printf("time: %s\n",   doc["time"].GetString()); }

        if(doc.HasMember("student_list") && doc["student_list"].IsArray()) 
        {
            int array_size = doc["student_list"].Size();
            rapidjson::Value &student_list = doc["student_list"];
            for(int i=0;i<array_size;i++)
            {
                rapidjson::Value &student = student_list[i];
                if(student.HasMember("name") && student["name"].IsString())
                {
                    printf("name: %s\n", student["name"].GetString());
                }
                if(student.HasMember("score") && student["score"].IsObject())
                {
                    rapidjson::Value &score = student["score"];
                    if(score.HasMember("chinese") && score["chinese"].IsString()) { printf("chinese: %s\n", score["chinese"].GetString()); }
                    if(score.HasMember("math")    && score["math"].IsString())    { printf("math: %s\n",    score["math"].GetString()); }
                    if(score.HasMember("english") && score["english"].IsString()) { printf("english: %s\n", score["english"].GetString()); }
                }
            }
        }
    }
}

const char* rapidjson_generate_json(void)
{
    rapidjson::StringBuffer StrBuf;
    rapidjson::Writer<rapidjson::StringBuffer>writer(StrBuf);

    printf("time: %s 1 %s\n", get_current_time().data(), get_current_time().c_str());

    writer.StartObject();
    writer.Key("theme");
    writer.String("student score");

    writer.Key("time");
    writer.String(get_current_time().data());

    writer.Key("student_list");
    writer.StartArray();

    //student 1
    writer.StartObject(); //student 1
    writer.Key("name");
    writer.String("Jerry");

    writer.Key("score");
    writer.StartObject(); //score
    writer.Key("chinese");
    writer.String("83");

    writer.Key("math");
    writer.String("82");

    writer.Key("english");
    writer.String("81");

    writer.EndObject();   //score
    writer.EndObject();   //student 1

    //student 2
    writer.StartObject(); //student 2
    writer.Key("name");
    writer.String("Harun");

    writer.Key("score");
    writer.StartObject(); //score
    writer.Key("chinese");
    writer.String("83");

    writer.Key("math");
    writer.String("82");

    writer.Key("english");
    writer.String("81");

    writer.EndObject();   //score
    writer.EndObject();   //student 2

    writer.EndArray();
    writer.EndObject();
    
    return StrBuf.GetString();
}